/**
 * 以勒咖啡机SDK适配器
 * 这个文件模拟了真实SDK的接口，便于在Web环境中开发和测试
 */

// 咖啡机串口管理器
export class CoffeeSerialPortManager {
    static instance = null;
    
    static getInstance() {
        if (!this.instance) {
            this.instance = new CoffeeSerialPortManager();
        }
        return this.instance;
    }
    
    constructor() {
        this.connected = false;
        this.serialPort = null;
    }
    
    initSerialPort(portName, name, baudRate, parity) {
        console.log(`初始化串口: ${portName}, 波特率: ${baudRate}`);
        this.serialPort = { 
            portName, 
            name, 
            baudRate, 
            parity 
        };
        return true;
    }
    
    openSerialPort() {
        console.log('打开串口连接');
        this.connected = true;
        return true;
    }
    
    closeSerialPort() {
        console.log('关闭串口连接');
        this.connected = false;
        return true;
    }
    
    isConnected() {
        return this.connected;
    }
}

// 咖啡机状态常量
export const CoffeeActionState = {
    COMMAND_SEND_SUCCESS: 0,
    COMMAND_SEND_FAILURE: 1,
    COMMAND_SEND_HAS_COMMAND: 2
};

// 咖啡机协议
export const CoffeeProtocol = {
    backCmdCode_A7: 0xA7, // 读取版本号
    backCmdCode_AB: 0xAB, // 所有故障
    // 其他协议代码...
};

// 咖啡机状态类
export class CoffeeMachineState {
    constructor(data = {}) {
        this.waterLevel = data.waterLevel || 80; // 水位 0-100
        this.beanLevel = data.beanLevel || 90;   // 豆量 0-100
        this.temperature = data.temperature || 95; // 当前温度
        this.error = data.error || null;     // 错误代码，null表示无错误
    }
}

// 咖啡包类
export class CoffeePackage {
    constructor() {
        this.cmdWord = 0;
        this.data = new Uint8Array(10);
        this.length = 0;
    }
    
    getCmdWord() {
        return this.cmdWord;
    }
}

// 检查状态动作类
export class CoffeeCheckStateAction {
    static instance = null;
    
    static getInstance() {
        if (!this.instance) {
            this.instance = new CoffeeCheckStateAction();
        }
        return this.instance;
    }
    
    constructor() {
        this.listener = null;
        this.intervalId = null;
    }
    
    setListener(listener) {
        this.listener = listener;
    }
    
    removeListener() {
        this.listener = null;
    }
    
    startCheckState(interval, delay) {
        this.stopCheckState();
        
        // 首次延迟后执行
        setTimeout(() => {
            this._checkState();
            
            // 然后按间隔执行
            this.intervalId = setInterval(() => {
                this._checkState();
            }, interval);
        }, delay);
    }
    
    stopCheckState() {
        if (this.intervalId) {
            clearInterval(this.intervalId);
            this.intervalId = null;
        }
    }
    
    _checkState() {
        if (!this.listener) return;
        
        // 模拟随机变化的状态
        const state = new CoffeeMachineState({
            waterLevel: Math.floor(Math.random() * 30) + 70, // 70-100
            beanLevel: Math.floor(Math.random() * 30) + 70,  // 70-100
            temperature: Math.floor(Math.random() * 10) + 90, // 90-100
            error: Math.random() < 0.05 ? '水箱缺水' : null  // 有5%概率报错
        });
        
        this.listener.onReceiveAllState(state);
    }
}

// 制作咖啡动作类
export class CoffeeMakeDrinkAction {
    static instance = null;
    
    static getInstance() {
        if (!this.instance) {
            this.instance = new CoffeeMakeDrinkAction();
        }
        return this.instance;
    }
    
    constructor() {
        this.listener = null;
        this.brewing = false;
    }
    
    setListener(listener) {
        this.listener = listener;
    }
    
    removeListener() {
        this.listener = null;
    }
    
    makeDrink(options) {
        if (!this.listener) return;
        
        if (this.brewing) {
            this.listener.onCmdSendState(CoffeeActionState.COMMAND_SEND_HAS_COMMAND);
            return;
        }
        
        this.brewing = true;
        this.listener.onActionStart();
        
        // 模拟发送命令成功
        setTimeout(() => {
            this.listener.onCmdSendState(CoffeeActionState.COMMAND_SEND_SUCCESS);
            
            // 模拟接收结果
            setTimeout(() => {
                const pkg = new CoffeePackage();
                pkg.cmdWord = 0x01; // 假设这是咖啡制作返回命令字
                this.listener.onReceiveResult(pkg);
                
                // 模拟动作完成
                setTimeout(() => {
                    this.brewing = false;
                    this.listener.onActionEnd();
                }, 5000);
            }, 1000);
        }, 500);
    }
}

// 停止制作咖啡动作类
export class CoffeeStopMakeDrinkAction {
    static instance = null;
    
    static getInstance() {
        if (!this.instance) {
            this.instance = new CoffeeStopMakeDrinkAction();
        }
        return this.instance;
    }
    
    stopMakeDrink() {
        console.log('停止制作咖啡');
        const makeDrinkAction = CoffeeMakeDrinkAction.getInstance();
        makeDrinkAction.brewing = false;
        return true;
    }
}

// 导出所有需要的类和常量
export default {
    CoffeeSerialPortManager,
    CoffeeCheckStateAction,
    CoffeeMakeDrinkAction,
    CoffeeStopMakeDrinkAction,
    CoffeeActionState,
    CoffeeMachineState,
    CoffeePackage,
    CoffeeProtocol
}; 